import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { CaptchaResult, LoginData, LoginResult } from "./types";

// /**
//  * 登录API
//  *
//  * @param data {LoginData}
//  * @returns
//  */
// export function loginApi(data: LoginData): AxiosPromise<LoginResult> {
//   const formData = new FormData();
//   formData.append("username", data.username);
//   formData.append("password", data.password);
//   formData.append("captchaKey", data.captchaKey || "");
//   formData.append("captchaCode", data.captchaCode || "");
//   return request({
//     url: "/api/v1/auth/login",
//     method: "post",
//     data: formData,
//     headers: {
//       "Content-Type": "multipart/form-data",
//     },
//   });
// }

/**
 * 登录API
 *
 * @param data {LoginData}
 * @returns
 */
export function loginApi(data: LoginData): AxiosPromise<LoginResult> {
  const formData = new FormData();
  formData.append("grant_type", "password");
  formData.append("scope", "all");
  formData.append("client_id", "client_trgzs");
  formData.append("client_secret", "123456");
  formData.append("username", data.username);
  formData.append("password", data.password);
  return request({
    url: "/oauth/token",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 注销API
 */
export function logoutApi() {
  return request({
    url: "/oauth/logout",
    method: "post",
  });
}

/**
 * 获取验证码
 */
export function getCaptchaApi(): AxiosPromise<CaptchaResult> {
  return request({
    url: "/api/v1/auth/captcha",
    method: "get",
  });
}

/**
 * 注册API
 * @param data {LoginData}
 * @returns
 */
export function register(data: LoginData) {
  const formData = new FormData();
  formData.append("username", data.username);
  formData.append("password", data.password);
  formData.append("name", data.name);
  formData.append("officeId", data.officeId);
  formData.append("post", data.post);
  formData.append("phone", data.phone);
  formData.append("sms_code", data.sms_code);
  formData.append("roleId", data.roleId);
  return request({
    url: "/oauth/register",
    method: "post",
    data: formData,
  });
}
/**
 * 发送手机验证码
 * @param data {LoginData}
 */
export function getPhoneCode(data: LoginData) {
  const formData = new FormData();
  formData.append("phone", data.phone);
  return request({
    url: "/static/sendSmsCode",
    method: "post",
    data: formData,
  });
}
